// const BASE_URL = "http://localhost:5173"
const BASE_URL = "http://8.146.206.32:8080"
//8.146.206.32:8080
export function request(config = {}) {
  let { url, method = 'GET', data = {} } = config
  url = BASE_URL + url

  return new Promise((resolve, reject) => {
    uni.request({
      url,
      method,
      data,
      success: res => {
        const { code, msg, data } = res.data || {}
        if (code === 1) {
          resolve(data)
        } else if (code === 0) {
          uni.showToast({
            title: msg || '请求失败',
            icon: 'none'
          })
          reject(res.data)
        } else {
          uni.showToast({
            title: '服务器返回未知状态',
            icon: 'none'
          })
          reject(res.data)
        }
      },
      fail: err => {
        uni.showToast({
          title: '网络连接失败',
          icon: 'none'
        })
        reject(err)
      }
    })
  })
}

// 封装 GET 请求
export function get(url, params = {}) {
  return request({ url, method: 'GET', data: params })
}

// 封装 POST 请求
export function post(url, data = {}) {
  return request({ url, method: 'POST', data })
}
